package Servlet;

import Service.UserService;
import bean.customer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/RegisterServlet")
public class RegisterServlet extends BaseServlet {


    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求参数
        req.setCharacterEncoding("UTF-8");
        String name = req.getParameter("name");
        String phonenumber = req.getParameter("phonenumber");
        String password = req.getParameter("password");

        UserService userService = new UserService();

        if (userService.existsUsername(name)) {
//            System.out.println("用户已存在");
            req.setAttribute("message","用户名已存在");
            req.setAttribute("name",name);
            req.setAttribute("phonenumber",phonenumber);
            req.setAttribute("password",password);
            req.getRequestDispatcher("/register.jsp").forward(req,resp);

        } else {
            //http://localhost:8080/Hospital_line_call_system_war_exploded/home_page.html
            userService.registerUser(new customer(null, name, phonenumber, password));
            req.setAttribute("message","注册成功，请重新登录");
            req.setAttribute("phonenumber",phonenumber);
            req.setAttribute("password",password);
            req.getRequestDispatcher("/login.jsp").forward(req, resp);

        }
    }
}
